package net.minhdiep.compassview.view;
/*
 * User: Minh Diep
 * Date: 6/8/13
 * Time: 12:15 AM
 */


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import net.minhdiep.compassview.R;

public class CompassView extends View {

    private float bearing;
    private float pitch;
    private float roll;

    private Paint markerPaint;
    private Paint textPaint;
    private Paint circlePaint;
    private Paint pointPaint;
    private String northStr="N";
    private String eastStr="E";
    private String southStr="S";
    private String westStr="W";
    private int textHeight;

    public float getBearing() { return bearing;}
    public void setBearing(float bearing) {
        this.bearing = bearing;
    }
    public float getPitch() {
        return pitch;
    }
    public void setPitch(float pitch) {
        this.pitch = pitch;
    }

    public float getRoll() {
        return roll;
    }
    public void setRoll(float roll) {
        this.roll = roll;
    }

    public CompassView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        initCompassView();

    }
    // Constructor required for inflation from resource file
    public CompassView (Context context, AttributeSet ats, int defaultStyle) {
        super(context, ats, defaultStyle );
        initCompassView();
    }
    //Constructor required for inflation from resource file
    public CompassView (Context context, AttributeSet attrs) {
        super(context, attrs);
        initCompassView();
    }


    @SuppressLint("ResourceAsColor")
    protected void initCompassView(){
        setFocusable(true);
        circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setColor(R.color.background_color);

        circlePaint.setStrokeWidth(3);
        circlePaint.setStyle(Paint.Style.STROKE);

        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(R.color.text_color);
        textPaint.setStrokeWidth(3);
        textHeight = (int) textPaint.measureText("yY");

        markerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        markerPaint.setColor(R.color.marker_color);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measuredWidth = measure(widthMeasureSpec);
        int measuredHeight = measure(heightMeasureSpec);
        int d = Math.min(measuredWidth, measuredHeight);
        setMeasuredDimension(d, d);
    }
    private int measure(int sizeLength) {
        // TODO Auto-generated method stub
        int result = 0;
        // Decode the measurement specifications.
        int specMode = MeasureSpec.getMode(sizeLength);
        int specSize = MeasureSpec.getSize(sizeLength);
        if (specMode == MeasureSpec.UNSPECIFIED) {
            // Return a default size of 200 if no bounds are specified.
            result = 200;
        } else {
            // As you want to fill the available space // always return the full available bounds.
            result = specSize;
        }
        return result;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int mMeasuredWidth = getMeasuredWidth();
        int mMeasuredHeight = getMeasuredWidth();

        int px = getMeasuredWidth()/2;
        int py = getMeasuredHeight()/2;
        int radius = Math.min(px, py) - 5;


        canvas.drawCircle(px, py, radius, circlePaint);
        canvas.save();
        canvas.rotate(-bearing, px, py);

        int textWidth = (int) textPaint.measureText("W");
        int cardX = px - textWidth/2;
        int cardY = py - radius + textHeight;

        for (int i = 0; i < 24; i++){
            // Draw a marker.
            canvas.drawLine(px, py - radius, px, py - radius + 10, markerPaint);
            canvas.save();
            canvas.translate(0, textHeight);

            if (i % 6 == 0){
                String dirStr = "";
                switch(i) {
                    case 0: dirStr= northStr;
                        int arrowY = 2 * textHeight;
                        canvas.drawLine(px, arrowY, px - 5, 3 * textHeight, markerPaint);
                        canvas.drawLine(px, arrowY, px + 5, 3 * textHeight, markerPaint);
                        break;
                    case 6: dirStr = eastStr;
                        break;
                    case 12: dirStr = southStr;
                        break;
                    case 18: dirStr = westStr;
                        break;
                }
                canvas.drawText(dirStr, cardX, cardY, textPaint);
            }
            else if (i % 3 == 0){
                // Draw the text every alternate 45deg
                String angle = String.valueOf(i * 15);
                float angleTextWidth = textPaint.measureText(angle);
                int angleTextX = (int) (px - angleTextWidth/2);
                int angleTextY = py - radius + textHeight;
                canvas.drawText(angle, angleTextX, angleTextY, textPaint);
            }
            canvas.restore();
            canvas.rotate(15, px, py);
        }
        canvas.restore();

//        RectF rollOval = new RectF((mMeasuredWidth/3)-mMeasuredWidth/7,
//                (mMeasuredHeight/2)-mMeasuredWidth/7,
//                (mMeasuredWidth/3)+mMeasuredWidth/7,
//                (mMeasuredHeight/2)+mMeasuredWidth/7
//        );
//        markerPaint.setStyle(Paint.Style.STROKE);
//        canvas.drawOval(rollOval, markerPaint);
//        markerPaint.setStyle(Paint.Style.FILL);
//        canvas.save();
//        canvas.rotate(roll, mMeasuredWidth/3, mMeasuredHeight/2);
//        canvas.drawArc(rollOval, 0, 180, false, markerPaint);
//
//        canvas.restore();
//        RectF pitchOval = new RectF((2*mMeasuredWidth/3)-mMeasuredWidth/7,
//                (mMeasuredHeight/2)-mMeasuredWidth/7,
//                (2*mMeasuredWidth/3)+mMeasuredWidth/7,
//                (mMeasuredHeight/2)+mMeasuredWidth/7);
//        markerPaint.setStyle(Paint.Style.STROKE);
//        canvas.drawOval(pitchOval, markerPaint);
//        markerPaint.setStyle(Paint.Style.FILL);
//        canvas.drawArc(pitchOval, 0-pitch/2, 180+(pitch), false, markerPaint);
//        markerPaint.setStyle(Paint.Style.STROKE);
    }
}
